import { BodySetup } from "utils/creep/CreepSetup";
import { ActionBase, ActionBaseOfCreep } from "../ActionBase";
import { priority } from "utils/constants/definetypes";
import { role_crossLevel } from "utils/constants/moveConstant";
import { StationNetwork } from "GameLoop/Path/StationNetwork";
import { EActionCode } from "framework/FSM/Action/ActionCode";
import { approach, hold_place } from "GameLoop/WorldState/mount/move/action.virtual";


/**
 * 采集Action
 */
/*@profile*/
export class ActionHarvest  extends ActionBaseOfCreep<IActionCMD<"harvest">>
{
    protected updateArgs(action: IActionCMD<"harvest">): EActionCode 
    {
        if(super.updateArgs(action) !== EActionCode.OK)
        {
            return EActionCode.FAILED;
        }
        action.arg.source = Game.getObjectById(action.arg.source.id) as Source;
        if(!action.arg.source)
        {
            return EActionCode.FAILED;
        }
        return EActionCode.OK;
    }
    protected _execute(action: IActionCMD<"harvest">): ActionCode 
    {
        const sourcepos = action.arg.source.pos;
        const creep = action.arg.entity;
        
        if(action.arg.stand)
        {
            if(action.arg.stand.distance(action.arg.entity.pos)>0)
            {
                StationNetwork.getInstance.PathFinder.UnRegisterBlockCreep(creep);
                action.arg.entity.mymoveTo({pos:action.arg.stand,range:0},{});
            }
            else
            {
                // 采集工开始干活了。就不允许动了
                approach(action.arg.entity,sourcepos,1);
                if(creep.store.getFreeCapacity()==0)
                {
                    return EActionCode.RUNNING;
                }
                
              
                action.arg.entity.harvest(action.arg.source);
            }
        }
        else
        {
            if(sourcepos.distance(action.arg.entity.pos)>1)
            {
                action.arg.entity.mymoveTo({pos:sourcepos,range:1},{});
            }
            else
            {
                // 采集工开始干活了。就不允许动了
                approach(action.arg.entity,sourcepos,1);
                if(creep.store.getFreeCapacity()==0)
                {
                    return EActionCode.RUNNING;
                }
            
                action.arg.entity.harvest(action.arg.source);
            }
        }
        return EActionCode.RUNNING;
    }
}